package com.factory.delete;
/**
 * @Author: dongyaoyao
 * @CreateTime: 2024-11-13
 * @Description:
 * @Version: 1.0
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Calendar;

@Service
public class DataCleanupService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    // 假设你的表名是`your_table`，并且有一个名为`created_at`的时间戳字段
    private static final String DELETE_OLD_DATA_SQL = "delete from socket_data where time < ?";

    @Scheduled(cron = "0 0 0 * * ?") // 每天午夜执行一次
    public void cleanupOldData() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -30);
        Timestamp threshold = new Timestamp(calendar.getTimeInMillis());
        jdbcTemplate.update(DELETE_OLD_DATA_SQL, threshold);
        System.out.println("Old data cleanup executed.");
    }
}